#include "stdafx.h"
#include "SortableObjArray.h"
#include "CUserFromFile.h"

int Comp(CUserFromFile* pFirstObj, CUserFromFile* pSecondObj)
	{
		if(pFirstObj->m_strSMTPAddress==pSecondObj->m_strSMTPAddress)
			return pFirstObj->m_strAccount<pSecondObj->m_strAccount;
		return pFirstObj->m_strSMTPAddress< pSecondObj->m_strSMTPAddress;
	}



#define STRIDE_FACTOR 3
void CSortableObArray::Sort()
{
	// This variation allows you to sort only a portion of the array
	
	ASSERT_VALID(this);
	int iElements =GetUpperBound() ;
	
	BOOL bFound;
	int iInner,iOuter,iStride = 1;
	CObject *pTmp;
	CObject **pData = &m_pData[0];
	
	while (iStride <= iElements)
		iStride = iStride * STRIDE_FACTOR + 1;
	
	while (iStride > (STRIDE_FACTOR - 1))
	{
		iStride = iStride / STRIDE_FACTOR;
		for (iOuter = iStride; iOuter < iElements; iOuter++)
		{
			bFound = 0;
			iInner = iOuter - iStride;
			while ((iInner >= 0) && !bFound)
			{
				if (Comp((CUserFromFile *)pData[iInner+iStride],(CUserFromFile *)pData[iInner]) < 0)
				{
					pTmp = pData[iInner+iStride];
					pData[iInner+iStride] = pData[iInner];
					pData[iInner] = pTmp;
					iInner -= iStride;
				}
				else
					bFound = 1;
			}
		}
	}
}





BOOL CSortableObArray::Store(CArchive &cAr)
{
	BOOL bOK=TRUE;
	try
		{
			Serialize(cAr);
		}
	catch(...)
		{
			bOK=FALSE;
		}
	return bOK;

}

int CSortableObArray::Load(CArchive &cAr)
{
	int ncount=-1;
	try
		{
			Serialize(cAr);
		}
	catch(...)
		{
		}
	return GetUpperBound();

}
